#include "wlibrary.h"
#include <QVBoxLayout>

WLibrary::WLibrary(Collection *col, QWidget *parent) :
    QWidget(parent)
{
    collection = col;
    this->setLayout(new QVBoxLayout(this));
    navigate = new WNavigate();
    this->layout()->addWidget(navigate);

    QObject::connect(collection, SIGNAL(ready()), this,
                     SLOT(addCollectionReady()));
    wTable = new WArtist();
    this->layout()->addWidget(wTable);
    wTable->setList((QList<AbstractData *> &)collection->getArtists());
    navigate->add(wTable);
    QObject::connect(wTable, SIGNAL(navigateToAlbums(QList<AbstractData *> &)),
                     this, SLOT(navAlbum(QList<AbstractData *>&)));
    QObject::connect(navigate, SIGNAL(navigation(WTable*)),this,
                     SLOT(navigation(WTable*)));

}

void WLibrary::addCollectionReady()
{
    // use refresh. Test later
//    if(wArtist->isVisible())
//    {
//        wArtist->setList(collection->getArtists());
//    } else if(wAlbum->isVisible())
//    {
//        wAlbum->setList(collection->getAlbums());
//    } else if(wTrack->isVisible())
//    {
//        wTrack->setList(collection->getTracks());
//    }
}

void WLibrary::navAlbum(QList<AbstractData *>& lAlbum)
{
    this->layout()->removeWidget(wTable);
    wTable->setVisible(false);
    wTable = new WAlbum();
    wTable->setList(lAlbum);
    wTable->setVisible(true);
    connect(wTable, SIGNAL(navigateToTracks(QList<AbstractData*>&)), this,
            SLOT(navTrack(QList<AbstractData *> &)));
    navigate->add(wTable);
    this->layout()->addWidget(wTable);
}

void WLibrary::navTrack(QList<AbstractData *> &lTrack)
{
    this->layout()->removeWidget(wTable);
    wTable->setVisible(false);
    wTable = new WTrack();
    wTable->setList(lTrack);
    wTable->setVisible(true);
//    connect(wTable, SIGNAL(navigateToTracks(QList<AbstractData*>&)), this,
//            navTrack(QList<AbstractData *> &));
    navigate->add(wTable);
    this->layout()->addWidget(wTable);
}

void WLibrary::navigation(WTable *wPrev)
{
    this->layout()->removeWidget(wTable);
    wTable->setVisible(false);
    wTable = wPrev;
    wTable->setVisible(true);
    this->layout()->addWidget(wTable);
}
